package com.xybbs.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xybbs.entity.Forums;
import com.xybbs.entity.Replies;
import com.xybbs.entity.Tips;
import com.xybbs.entity.Users;
import com.xybbs.service.ForumsService;
import com.xybbs.service.RepliesService;
import com.xybbs.service.TipsService;

@RequestMapping("tips")
@Controller
public class TipController {

	@Autowired
	ForumsService forumsService;
	@Autowired
	TipsService tipsService;
	@Autowired
	RepliesService repliesService;


	@RequestMapping("toPublishTipPage.do")
	public String toPublishTipPage(HttpServletRequest req) {
		List<Forums> list = forumsService.checkAll();
		req.setAttribute("forums", list);
		return "publishTip";
	}
	
	@RequestMapping("publishNewTip.do")
	public String publishNewTip(Tips tip,HttpServletRequest req) {
		tip.setTipModifyTime(new Date());
		tip.setTipPublishTime(new Date());
		tipsService.add(tip);
		req.setAttribute("myInfo", 0);
		return "publishTip";
	}
	
	@RequestMapping("showTip.do")
	public String showTip(String tipId,HttpServletRequest req) {
		Tips tip = tipsService.checkTipByIdWithUser(tipId);
		tipsService.updateTipByIdWhenClicked(tipId);
		List<Replies> list = repliesService.checkAllByTipIdWithUser(tipId);
		req.setAttribute("replies", list);
		req.setAttribute("tip", tip);
		return "tipContent";
	}
	
	@GetMapping("toTipManagePage.do")
	public String toTipManagePage(HttpServletRequest req) {
		List<Tips> list = tipsService.checkAllWithUser();
		req.setAttribute("tips", list);
		return "tipManage";
	}

	@GetMapping("changeTipStatus.do")
	public String ChangeTipStatus(String tipId,String option,String code) {
		if(option.equals("1")) {
			tipsService.updateTipStatus(tipId, code);
		}else {
			option = "tipIsKnot";
			tipsService.updateTipKnow(tipId, code);
		}
		return "redirect:toTipManagePage.do";
	}
	

	

}
